using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// La clase tiene una lista de templates con los datos de cada arma.
/// Despues llamando al metodo create te devuelve una weapon con el template que le pedis.
/// </summary>
public class WeaponManager
{

    #region Singleton
    private static WeaponManager instance;

    public static WeaponManager GetInstance()
    {
        if (instance == null)
        {
            instance = new WeaponManager();
        }
        return instance;
    }

    private WeaponManager()
    {
    }
    #endregion


    List<WeaponTemplate> templates = new List<WeaponTemplate>();
    bool m_WeaponCreated = false;
    public void AddTemplate(WeaponTemplate template)
    {
        templates.Add(template);
    }
    private WeaponTemplate GetTemplate(string id)
    {
        for (int i = 0; i < templates.Count; i++)
        {
            if (templates[i].id == id)
            {
                return templates[i];
            }

        }
        return null;
    }


    /// <summary>
    /// Crea un Weapon, devuelve null si el template no existe.
    /// </summary>
    /// <param name="id">Nombre del template</param>
    /// <param name="isEnemy">Si el arma la creo un enemigo, si la crea un enemigo tiene rapidfire y balas infinitas.</param>
    /// <returns>Devuelve null si no existe el template</returns>
    public WeaponBehaviour Create(string id,bool isEnemy)
    {
        WeaponTemplate template = GetTemplate(id);
        if (template == null) return null;

        WeaponBehaviour weapon;
        if (isEnemy)
        {
            weapon = new WeaponBehaviour(template, Enemy.c_EnemyLayer);
            weapon.m_RapidFire = true;
            weapon.m_InfiniteBullets = true;

        }
        else
        {
            weapon = new WeaponBehaviour(template, Character.c_CharacterLayer);
        }

        return weapon;
    }
    /// <summary>
    /// Aca se crean los templates de cada arma del juego.
    /// </summary>
    public void CreateWeaponsTemplate()
    {
        if (m_WeaponCreated) return;


        WeaponTemplate template;
        //--------------------------Creacion de pistola-------------------------------------------
        template = new WeaponTemplate();
        template.id = "pistol";
        template.capacityMagazine = 12;
        template.damage = 50;
        template.fireRate = 0.4f;
        template.equipTime = 1.0f;
        template.Range = 40;
        template.RapidFire = false;
        template.Noise = 50;

        template.visualModelGUI = PrefabManager.GetInstance().GetPrefab("pistolGUI");
        template.visualModelWorld = PrefabManager.GetInstance().GetPrefab("standardbox");
        template.inventorySpace = new bool[2, 3];
        template.RequieredPerk = "";
        template.SoundName = "Pistolshoot";

        template.ToolTip = "Pistola basica. No requiere perk para equiparla \n Range: " + template.Range + "\n Damage: " + template.damage + "\n Type: " + template.id;

        WeaponManager.GetInstance().AddTemplate(template);
        //--------------------------Creacion de rifle-------------------------------------------
        template = new WeaponTemplate();
        template.id = "rifle";
        template.capacityMagazine = 30;
        template.damage = 20;
        template.fireRate = 0.15f;
        template.equipTime = 1.0f;
        template.Range = 55;
        template.RapidFire = true;
        template.Noise = 50;

        template.visualModelGUI = PrefabManager.GetInstance().GetPrefab("rifleGUI");
        template.visualModelWorld = PrefabManager.GetInstance().GetPrefab("standardbox");
        template.inventorySpace = new bool[4, 6];
        template.RequieredPerk = Perks.RifleProficiency.Name;

        template.ToolTip = "Rifle basico. Requiere perk para equiparla, se puede mantener el click para disparar\n Range: " + template.Range + "\n Damage: " + template.damage + "\n Type: " + template.id;
        template.SoundName = "Pistolshoot";

        WeaponManager.GetInstance().AddTemplate(template);



        //--------------------------Creacion de sniper-------------------------------------------
        template = new WeaponTemplate();
        template.id = "sniper";
        template.capacityMagazine = 3;
        template.damage = 100;
        template.fireRate = 2.0f;
        template.equipTime = 1.0f;
        template.Range = 100;
        template.RapidFire = false;
        template.Noise = 100;

        template.visualModelGUI = PrefabManager.GetInstance().GetPrefab("sniperGUI");
        template.visualModelWorld = PrefabManager.GetInstance().GetPrefab("standardbox");
        template.inventorySpace = new bool[3, 10];
        template.RequieredPerk = Perks.SniperProficiency.Name;
        template.SoundName = "Snipershoot";

        template.ToolTip = "Sniper basico. Requiere perk para equiparla \n Range: " + template.Range + "\n Damage: " + template.damage + "\n Type: " + template.id;

        WeaponManager.GetInstance().AddTemplate(template);
    }
}
